﻿using Cudafy;
using Cudafy.Host;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Lights
{
    static class Profiling
    {
        public static void UpdateUserProfile()
        {
            try
            {
                NameValueCollection pairs = new NameValueCollection();
                int hashcode = (Environment.MachineName.GetHashCode() ^ Environment.UserName.GetHashCode());
                // ottengo un codice hash univoco per questo computer
                pairs.Add("hashcode", hashcode.ToString());

                // ottengo le informazioni sul computer in uso
                Dictionary<string, string> profile = new Dictionary<string, string>();
                profile.Add("is64bit", Environment.Is64BitProcess.ToString());
                profile.Add("processorscount", Environment.ProcessorCount.ToString());
                profile.Add("opencldevicescount", CudafyHost.GetDeviceCount(eGPUType.OpenCL).ToString());

                // aggiungo il dizionario ai dati da inviare
                pairs.Add("profile", JsonConvert.SerializeObject(profile));

                // invio i dati sul server
                using (WebClient client = new WebClient())
                {
                    var response = client.UploadValues("http://scarecrowstudios.altervista.org/lights/insert.php", pairs);
                    //if (Encoding.ASCII.GetString(response).Contains(hashcode.ToString()))
                    //    MessageBox.Show("inserimento riuscito");
                }
            } catch { }
        }

        public static void ReportError(Exception e)
        {
            try
            {

                NameValueCollection pairs = new NameValueCollection();
                int hashcode = (Environment.MachineName.GetHashCode() ^ Environment.UserName.GetHashCode());
                // ottengo un codice hash univoco per questo computer
                pairs.Add("hashcode", hashcode.ToString());

                // serializzo l'eccezione
                string exc = JsonConvert.SerializeObject(e);
                pairs.Add("error", exc);

                // ottengo un id univoco per l'eccezione
                int iderrors = exc.GetHashCode();
                pairs.Add("iderrors", iderrors.ToString());

                pairs.Add("solved", false.ToString());

                // invio i dati sul server
                using (WebClient client = new WebClient())
                {
                    var response = client.UploadValues("http://scarecrowstudios.altervista.org/lights/logerror.php", pairs);
                    //if (Encoding.ASCII.GetString(response).Contains(hashcode.ToString()))
                    //    MessageBox.Show("inserimento riuscito");
                }
            } catch { }
        }
    }
}
